#ifndef _I_ADVISE_EVENT_IMPL_H
#define _I_ADVISE_EVENT_IMPL_H

#pragma warning(disable: 4786)

#include <vector>

/**
 * @class TOLAdviseEventImpl
 * 
 * @brief In order to manage object event, <br>
 * All objects which need manage event can base on it.
 */
template <class T>
class TOLAdviseEventImpl
{

public:

	/// Destructor
	virtual ~TOLAdviseEventImpl() {}

	/// Advise a event's pointer,
	/// <b>Note.</b>Return FALSE, if "p" is a exist event or null   
	BOOL Advise(T* p)
	{
		if (!p)  return FALSE;

		for ( size_t index = 0; index < m_EventVec.size(); index++ )
		{
			if (m_EventVec[index] == p)
				return FALSE;
		}
		m_EventVec.push_back(p);
		return TRUE;
	}

	/// Unadvise from manager.
	/// <b>Note.</b>Return FALSE, if "p" is not in manager or null   
	BOOL UnAdvise(T* p)
	{
		std::vector<T*>::iterator i = m_EventVec.begin();

		if (!p) return FALSE;

		for ( ; i != m_EventVec.end(); i++ )
		{
			if (*i == p)
			{
				m_EventVec.erase(i);
				return TRUE;
			}
		}
		return FALSE;
	}

	/// Get event by index
	T* GetEvent(int index)
	{
		if (((size_t)index >= m_EventVec.size()) || index < 0)
			return NULL;
		return m_EventVec[(size_t)index];
	}

	/// Get event count
	int GetEventCount() const
	{
		return ( (int) m_EventVec.size() );
	}

private:

	/// Manager variable
	std::vector<T*>	m_EventVec;
};

#endif // #define _I_ADVISE_EVENT_IMPL_H